var _mobile_reg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
var _wx_server_qr_code_count = 0;
var _wx_server_qr_code_loaded = false;
var _qr_code_limited = '';
var _qr_code_wait_time = 20;
var flashQrCodeWaitingTimer = null;
var getQrCodeStatusTimer = null;
var getQrCodeTimer = null;

/**
 * 根据密码登录
 */
$("#normalSubmit").click(function () {
    if (nameLoginCheck()){
        closeError();
        $.ajax({
            url: urlAuth + "itripUser/login",
            data:{"phone":$("#normalUser").val(),"password":$("#normalPassword").val()},
            type:"post",
            success:function (data) {
                if (data.success){
                    sessionStorage.setItem("token",data.data.token);
                    sessionStorage.setItem("loginUser",JSON.stringify(data.data.user));
                    location.href="page/index/index.html";
                }else{
                    showError(data.message);
                }
            }
        })
    }
});
/**
 * 根据验证码登录
 */
$("#partnerSubmit").click(function () {
    if (mobileLoginCheck()){
        closeError();
        $.ajax({
            url: urlAuth + "itripUser/login",
            data:{"phone":$("#partnerPhone").val(),"code":$("#partnerJym").val(),"page":1},
            type:"post",
            success:function (data) {
                if (data.success){
                    console.log(data);
                    sessionStorage.setItem("token",data.data.token);
                    sessionStorage.setItem("loginUser",JSON.stringify(data.data.user));
                    location.href="page/index/index.html";
                }else{
                    showError(data.message);
                }
            }
        })
    }
});

/**
 * 密码登录验证
 * @returns {boolean}
 */
function nameLoginCheck(){
    var loginName = $("#normalUser").val();
    var password = $("#normalPassword").val();
    if($(".tips ").is(":visible")){
        return false;
    }
    if(loginName == null  || loginName == ""){
        showError("请输入账号");
        return false;
    }
    if(password == null  || password == ""){
        showError("请输入密码");
        return false;
    }
    if($("#normalYzm") && $("#nameLoginForm").find("#normalYzm").length > 0 ){
        if($("#normalYzm").val() == "" || $("#normalYzm").val() == null){
            showError("请输入验证码");
            return false;
        }
    }
    return true;
}

/**
 * 手机登陆验证
 * @returns {boolean}
 */
function mobileLoginCheck(){
    var mobile = $("#partnerPhone").val();
    var code = $("#partnerJym").val();
    if(mobile == null || mobile == '' || !(_mobile_reg).test(mobile)){
        showError("请填写正确的手机号");
        return false;
    }
    if(code == null || code == ""){
        showError("请填写校验码");
        return false;
    }
    return true;
}

/**
 * 校验手机格式
 * @param obj
 * @returns {boolean}
 */
function mobileCheck(obj){
    if(!(_mobile_reg).test($("#partnerPhone").val())){
        showError("请填写正确的手机号");
        return false;
    }else{
        return true;
    }
}


/**
 * 获取验证码倒计时
 */
var timer=null;
var countDown = 59;
function time() {
    clearInterval(timer);
    console.log("checkcccccccccccccc");
    timer = setInterval(function () {
        if (countDown == 0) {
            $("#smsSendButton").attr('disabled', false);
            $("#smsSendButton").css({"pointer-events":"auto","opacity": 1});
            $("#smsSendButton").html('发送验证码');
            clearInterval(timer);
        } else {
            $("#smsSendButton").attr("disabled", true);
            $("#smsSendButton").css({"pointer-events":"none","opacity": 0.5});
            $("#smsSendButton").html(countDown + "s后重新发送");
            countDown--;
        }
    }, 1000);
}

/**
 * 获取验证码
 */
function sendSms(){
    var phone = $("#partnerPhone").val();
    if ((_mobile_reg).test(phone)){
        closeError();
        time();
        $.ajax({
            url: urlAuth + "itripUser/getCode",
            data:{"phone":phone,"page":1},
            type:"get",
            success:function (data) {
                if (data.success==false){
                    showError(data.message);
                }
            }
        })
    }else{
        showError("请填写正确的手机号");
    }
}

$(function(){
    $(".form-tab li").on("click",function(){
        var index = $(this).index();
        $(this).addClass("cur").siblings().removeClass("cur");
        $(".form-con>div").hide().eq(index).show();
        if(index == 0){
            $(".form-foot").hide();
        }else{
            $(".form-foot").show();
        }
        $(".form-error").hide();
    });
    $(".weixin-login .help-a").hover(
        function(){
            $(".wx-img-box,.wx-image").stop();
            $(this).parents(".weixin-login").find(".wx-img-box").animate({"marginLeft":"15px"},300,function(){
                $(this).parents(".weixin-login").find(".wx-image").animate({"opacity":1},300);
            });
        },
        function(){
            $(".wx-img-box,.wx-image").stop();
            $(this).parents(".weixin-login").find(".wx-image").stop().animate({"opacity":0},300,function(){
                $(this).parents(".weixin-login").find(".wx-img-box").animate({"marginLeft":"110px"},300);
            });
        }
    );


});
$('.jia_foot_open').click(function(){
    $('.footnone').slideToggle();
    $(this).find('i').toggleClass('footnow');
});
$('.phoneLogin').click(function(){
    $('.loginV2').hide();
    $('.shortLogin').show();
    $('.form-error').hide();
});
$('.backLogin').click(function(){
    $('.login-normal').show();
    $('.loginV2').show();
    $('.shortLogin').hide();
    $('.form-error').hide();
});
//开启错误提示
function showError(error){
    $(".form-error").find("label").html(error);
    $(".form-error").show();
}
//关闭错误提示
function closeError() {
    $(".form-error").hide();
}